package com.ts.api.module.match.market.internal.sort.handler;

import com.gitee.apanlh.util.base.Empty;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.module.channel.entity.po.ChannelPO;
import com.ts.api.module.match.market.internal.sort.context.ChannelMarketSortContext;
import com.ts.api.module.match.market.internal.sort.model.ChannelMarketSort;
import com.ts.api.module.match.market.internal.sort.model.ChannelMarketSortResult;
import com.ts.api.module.match.market.internal.sort.strategy.ChannelMarketSortStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *  渠道贷超排序处理器
 *
 *  @author Pan
 */
@Component
public class ChannelMarketSortHandler {

    @Autowired
    private ChannelMarketSortContext channelMarketSortContext;

    /**
     *  结果排序处理器
     *
     *  @author Pan
     *  @param  channelMarketSort       贷超排序实体
     *  @return ChannelMarketSortResult 贷超排序返回实体
     */
    public ChannelMarketSortResult handler(ChannelMarketSort channelMarketSort) {
        ChannelPO channelPO = channelMarketSort.getChannelPO();
        Integer marketDisplayMode = channelPO.getMarketDisplayMode();

        if (ValidParam.isEmpty(channelMarketSort.getListMarket())) {
            ChannelMarketSortResult channelMarketSortResult = new ChannelMarketSortResult();
            channelMarketSortResult.setListMarketSort(Empty.list());
            return channelMarketSortResult;
        }

        ChannelMarketSortStrategy context = channelMarketSortContext.getContext(marketDisplayMode);
        return context.sort(channelMarketSort);
    }
}
